---@class SkillEffect372102:BaseSkillEffect
SkillEffect372102 = ClientFight.CreateClass("SkillEffect372102", ClientFight.BaseSkillEffect)
local t = SkillEffect372102
---@param skill FightUsingSkill
function t:effectSkill(skill, triggerFrame, skillUniqueId)
    -- 单体目标验证一下是否死亡 范围伤害找一下溅射目标
    local targets = self:selectDamageTargets(skill, nil);
    -- 是否命中目标
    local hit = false;
    for k, _target in pairs(targets) do
        table.insert(skill.actionTargets, _target);
        local rootSkill = SkillManager.getRootSkill(skill);
        table.insert(rootSkill.actionTargets, _target)
        -- 这里是为了保证整个技能期间只对目标伤害一次
        if (skill.skill.skillBean.f_TargetRepeat == SkillTargetRepeatEnum.UNREPEATABLE_ALL) then
            local _skill = skill;
            while (_skill.parentSkill ~= nil and _skill.parentSkill ~= _skill) do
                _skill = _skill.parentSkill
                table.insert(_skill.actionTargets, _target);
            end
        end
        -- 攻击
        hit = bit.bor(hit, DamageManager.attack(skill, _target, triggerFrame))
    end

    if (hit) then
        -- 技能触发技能
        if skill.triggerFrames[SkillManager.HIT_TRIGGER] then
            local triggerSkills = skill.triggerFrames[SkillManager.HIT_TRIGGER]
            for k, triggerSkill in pairs(triggerSkills) do
                SkillManager.triggerSkill(skill, nil, triggerSkill)
            end
        end
        SkillManager.checkAndCreateSummon(SkillManager.HIT_TRIGGER, skill)
    end
    return true;
end
---@param skill FightUsingSkill
function t:selectDamageTargets(skill, center)
    local fight = skill.fight
    local fighter = skill.fighter
    local skillBean = skill.skill.skillBean
    local skillId = skillBean.f_SkillID

    -- 单体伤害
    if (skillBean.f_AreaShape == SkillTargetAreaShapeEnum.SINGLE) then

        -- 矩形伤害
    elseif skillBean.f_AreaShape == SkillTargetAreaShapeEnum.RECTANGLE then
        --中心点
        if (center == nil) then
            center = TargetAreaCenterType.type4:action(skill);
        end
        local logCenter = center;
        local enemyList = fight.teams[3 - fighter.teamIndex].fighters
        local selectors = {}
        for i = 1, table.getn(enemyList) do
            local enemy = enemyList[i];
            self:EnemyLoop(enemy, skillBean, skill, fight, fighter, center, selectors)
        end
        if (#selectors == 0) then
            return selectors;
        end

        local selected = TargetRuleSelector.action(fighter, skill.skill.targetRule, selectors, skill.skill.targetRuleParam)
        if (#selected == 0) then
            return selected;
        end

        local skillEffect = SkillManager.getSkillEffect(skill.skill.skillBean.f_SkillScript)
        local targetMax = skillBean.f_TargetMax
        if (skillEffect ~= nil) then
            targetMax = skillEffect:getTargetMax(skill);
        end
        local subList = {}
        local minNum = math.min(targetMax, #selected)
        for k = 1, minNum do
            table.insert(subList, selected[k])
        end
        return subList
    end
    return {}
end
---@param enemy Fighter
---@param skillBean T_skillBean
---@param skill FightUsingSkill
---@param fight Fight
---@param fighter Fighter
function t:EnemyLoop(enemy, skillBean, skill, fight, fighter, center, selectors)
    -- 移除死亡
    if (enemy:isDie()) then
        return
    end
    -- 移除召唤物
    if (skillBean.f_AimTarget == 1 and enemy.summons) then
        return
    end
    -- 移除英雄
    if (skillBean.f_AimTarget == 2 and not enemy.summons) then
        return
    end
    if (skillBean.f_AimTarget == 3) then
        if (enemy.summons or enemy.heroBean.f_Type == 3) then
            return
        end
    end
    if (skillBean.f_Target == SkillTargetTypeEnum.TARGET) then
        if (FighterStateEnum.is(enemy, FighterStateEnum.CAN_NOT_DAMAGE_BY_ENEMY)) then
            SkillRoleMgr.savePathFile("[" .. fight.frame .. "][SKILL]" .. fighter.fighterId .. "的" .. skillId .. "选择伤害目标" .. enemy.fighterId .. "因为不可被敌方伤害而排除");
            return
        end
        -- 不可被普攻选中
        if (FighterStateEnum.is(enemy, FighterStateEnum.CAN_NOT_SELECT_BY_NORMAL_ATTACK) and skill.skill.skillBean.f_SkillType == SkillTypeEnum.NORMAL_ATTACK) then
            SkillRoleMgr.savePathFile("[" .. fight.frame .. "][SKILL]" .. fighter.fighterId .. "的" .. skillId .. "选择伤害目标" .. enemy.fighterId .. "因为不可被敌方 普攻伤害 而排除");
            return ;
        end
        local abs = skill.skill.fighterSelectStates[enemy.fighterId]
        if (abs == nil) then
            skill.skill.fighterSelectStates[enemy.fighterId] = false
            local fightResult = FightResult.New();
            EventManager.notifyEventListener(fight, FightEventEnum.BE_SELECT, enemy.fighterId, fightResult)
            if (fightResult:getState() == DamageResultTypeEnum.IMMUNITY) then
                --被免疫掉了本次被选中
                skill.skill.fighterSelectStates[enemy.fighterId] = true
                return
            end
        elseif (abs == true) then
            SkillRoleMgr.savePathFile("[" .. fight.frame .. "][SKILL]" .. fighter.fighterId .. "的" .. skillId .. "选择伤害目标" .. enemy.fighterId .. "因为免疫本次选中而排除");
            return
        end
    end
    if (skillBean.f_Target == SkillTargetTypeEnum.FRIEND) then
        if (FighterStateEnum.is(enemy, FighterStateEnum.CAN_NOT_DAMAGE_BY_FRIENDLY) or enemy.fighterId == fighter.gfighterId) then
            SkillRoleMgr.savePathFile("[" .. fight.frame .. "][SKILL]" .. fighter.fighterId .. "的" .. skillId .. "选择伤害目标" .. enemy.fighterId .. "因为不可包含自己而排除/不可被友方伤害而排除");
            return
        end
    elseif (skillBean.f_Target == SkillTargetTypeEnum.FRIEND_AND_ONESELF) then
        if (enemy ~= fighter) then
            if (FighterStateEnum.is(enemy, FighterStateEnum.CAN_NOT_DAMAGE_BY_FRIENDLY)) then
                SkillRoleMgr.savePathFile("[" .. fight.frame .. "][SKILL]" .. fighter.fighterId .. "的" .. skillId .. "选择伤害目标" .. enemy.fighterId .. "因为不可被友方伤害而排除");
                return
            end
        end
    end
    if (skillBean.f_TargetRepeat == SkillTargetRepeatEnum.UNREPEATABLE) then
        if (table.contains(skill.actionTargets, enemy)) then
            SkillRoleMgr.savePathFile("[" .. fight.frame .. "][SKILL]" .. fighter.fighterId .. "的" .. skillId .. "选择伤害目标" .. enemy.fighterId .. "因为被伤害过而排除");
            return
        end
    end

    if (skillBean.f_TargetRepeat == SkillTargetRepeatEnum.UNREPEATABLE_ALL) then
        if table.contains(skill.actionTargets, enemy) then
            SkillRoleMgr.savePathFile("[" .. fight.frame .. "][SKILL]" .. fighter.fighterId .. "的" .. skillId .. "选择伤害目标" .. enemy.fighterId .. "因为被伤害过而排除");
            return
        end
        local rootSkill = SkillManager.getRootSkill(skill);
        if (table.contains(rootSkill.actionTargets, enemy)) then
            SkillRoleMgr.savePathFile("[" .. fight.frame .. "][SKILL]" .. fighter.fighterId .. "的" .. skillId .. "选择伤害目标" .. enemy.fighterId .. "因为被伤害过而排除");
            return
        end
    end
    -- 标记选择
    local signs = skillBean.f_TagActivate
    if (signs ~= nil) then
        if (not SkillManager.skillTargetSignCheck(enemy, signs)) then
            SkillRoleMgr.savePathFile("[" .. fight.frame .. "][SKILL]" .. fighter.fighterId .. "的" .. skillId .. "选择伤害目标" .. enemy.fighterId .. "因为没有标记而排除");
            return
        end
    end

    -- 距离计算
    if (DistanceUtil.overlap(center, skill.skill.damageShape, enemy.position, enemy.shape)) then
        SkillRoleMgr.savePathFile("[" .. fight.frame .. "][SKILL]" .. fighter.fighterId .. "的" .. skillId .. "选择伤害目标" .. enemy.fighterId .. "坐标" .. enemy.position .. "成功");
        table.insert(selectors, enemy);
    else
        SkillRoleMgr.savePathFile(string.format("[%s][SKILL]%s的%s选择伤害目标%s坐标:%s形状:%s,因为在伤害范围外而排除,技能中心点%s,形状%s", fight.frame, fighter.fighterId, skillId, enemy.fighterId, enemy.position, enemy.getShape(), logCenter, skill.skill.getDamageShape()));
    end

end
function t:triggerSkill(parentSkill, target, triggerSkill)
    local fighter = parentSkill.fighter;
    --在主人当前的位置,创建一个虚拟单位
    target = createTempFighter(fighter.fight, fighter.fight.teams[3 - fighter.teamIndex], fighter.position);
    local fight = parentSkill.fight;
    local skillBean = triggerSkill.skill.skillBean;
    --对主人当前的位置放个技能
    -- 整个技能不可重复目标
    if (skillBean.f_SkillPointRepeat == SkillPointRepeatEnum.UNREPEATABLE_ALL) then
        table.insert(parentSkill.excludes, parentSkill.target.fighterId);
    end
    -- 把所有打过的目标都记在最上面的技能上面
    --      System.out.println("parentSkill:" + parentSkill.skill.skillId);
    if (parentSkill.target ~= nil) then
        local rootSkill = ClientFight.SkillManager.getRootSkill(parentSkill);
        table.insert(rootSkill.attackedTargets, parentSkill.target.fighterId);
        if (parentSkill ~= rootSkill) then
            -- 父技能上面也记一个
            table.insert(parentSkill.attackedTargets, parentSkill.target.fighterId);
        end
    end

    -- 触发技能选择目标
    if (target == nil) then
        if (skillBean.f_TargetRule == SkillTargetRuleEnum.PARENTSKILL_TARGET) then
            target = parentSkill.target;
            -- 目标死亡时重新选择目标（skill.skill==nil为buff触发）
            if (target:isDie() and skillBean.f_dieContinue == SkillContinueTypeEnum.RESELECTION_ON_TARGETDIE and parentSkill.skill ~= nil) then
                local parentSkillBean = parentSkill.skill.skillBean;
                if (parentSkillBean.f_SkillPointRepeat ~= SkillPointRepeatEnum.REPEATABLE) then
                    target = skillManager.selectAttackTarget(fight, fighter, parentSkill.skill, parentSkill.position, parentSkill.excludes, true);
                else
                    target = skillManager.selectAttackTarget(fight, fighter, parentSkill.skill, parentSkill.position, nil, true);
                end
            else
                --                    -- 吸引普攻子弹
                --                    if (skillBean.getF_SkillType() == 1 && skillBean.getF_BulletTpye() == 1) {
                --                        List<Fighter> others = FighterUtils.getOthersBy(fighter, true, nil);
                --                        for (Fighter other : others) {
                --                            if (FighterStateEnum.is(other, FighterStateEnum.ATTRACT_NORMAL_BULLET)) {
                --                                target = other;
                --                                break;
                --                            }
                --                        }
                --                    }
            end
        elseif (skillBean.f_SkillPointRepeat ~= SkillPointRepeatEnum.REPEATABLE) then
            target = skillManager.selectAttackTarget(fight, fighter, triggerSkill.skill, parentSkill.position, parentSkill.excludes, true);
        else
            target = skillManager.selectAttackTarget(fight, fighter, triggerSkill.skill, parentSkill.position, nil, true);
        end
    end

    if (target == nil) then
        local skillId = 0;
        if (parentSkill.skill ~= nil) then
            skillId = parentSkill.skill.skillBean.f_SkillID;
        end
        local logSkill = skillId;
        SkillRoleMgr.savePathFile("[" .. fight.frame .. "][SKILL]" .. fighter.fighterId .. "父技能" .. logSkill .. "子技能" .. triggerSkill.skill.skillId .. "因为没有目标触发技能失败");
        return SkillEffectResult.New(true, false);
    end

    -- 死亡后不继续释放
    if (target:isDie() and skillBean.f_dieContinue == SkillContinueTypeEnum.STOP_ON_TARGETDIE) then
        SkillRoleMgr.savePathFile("[" .. fight.frame .. "][SKILL]" .. fighter.fighterId .. "父技能" .. parentSkill.skill == nil and "0" or parentSkill.skill.skillBean.f_SkillID .. "子技能" .. triggerSkill.skill.skillId .. "因为没有目标死亡技能失败");
        return SkillEffectResult.New(true, false);
    end

    triggerSkill.target = target;

    -- 整个技能不可重复目标
    if (skillBean.f_SkillPointRepeat == SkillPointRepeatEnum.UNREPEATABLE_ALL) then
        triggerSkill.excludes = parentSkill.excludes;
    end

    -- 目标不可重复
    if (skillBean.f_SkillPointRepeat ~= SkillPointRepeatEnum.REPEATABLE) then
        table.insert(parentSkill.excludes, target.fighterId);
    end
    triggerSkill.startPosition = parentSkill.position;
    -- 如果非分离技能，打断所有正在使用的非分离技能
    if (skillBean.f_SkillOut ~= 1 and fighter.usingSkill ~= nil) then
        ClientFight.SkillManager.finishSkill(fighter.usingSkill, true, FinishSkillReasonEnum.USE_SKILL);
    end
    triggerSkill.setOriginalTargetPlacePosition(Vector2.New(target.position.x, target.position.y));
    if (skillBean.f_SkillOut == 1) then
        local frameInfoCommand = ReportManager.flyToJsonString(fight, fighter, target, triggerSkill);
        table.insert(fight.actions, frameInfoCommand);
        FightManager.playActions(frameInfoCommand, triggerSkill)
    else
        local attack = ReportManager.attackToJsonString(fight, fighter, target, triggerSkill, triggerSkill.speed);
        table.insert(fight.actions, attack);
        FightManager.playActions(attack, skill)
        if (skillBean.f_Speed ~= 0 or skillBean.f_moveFrame ~= 0) then
            fighter.moveAttackAction = attack;
        end
    end

    skillManager.addSkill(fight, triggerSkill);
    local logTarget = target;
    SkillRoleMgr.savePathFile("[" .. fight.frame .. "][SKILL]" .. fighter.fighterId .. "对" .. logTarget.fighterId .. "触发子技能" .. triggerSkill.skill.skillBean.f_SkillID);

    -- 开始释放技能
    ClientFight.SkillManager.prepareSkill(triggerSkill, parentSkill.position);

    -- 子技能加速
    if (parentSkill.frame < parentSkill.skipFrame) then
        triggerSkill.skipFrame = parentSkill.skipFrame - parentSkill.frame;
    end
    -- 聚光灯播放效果
    if triggerSkill.stage ~= FightSkillStageEnum.FINISH and triggerSkill.frame < triggerSkill.skipFrame then
        fight.addFrozenSkills(triggerSkill);
    end
    return SkillEffectResult.New(true, true);
end

function t:createTempFighter(fight, enemyTeam, pos)
    local fighter = Fighter.New();
    fighter.fight = fight;
    fighter.fightTeam = enemyTeam;
    fighter.teamIndex = enemyTeam.teamIndex;
    fighter.fighterId = fight.ids;
    fight.ids = fight.ids + 1;
    fighter.leader = false;

    -- 模型ID
    fighter.modelId = 9999;
    fighter.star = 1;
    fighter.level = 1;
    fighter.position = pos;
    local baseState = BaseState.New();
    baseState.state = bit.bor(baseState.state, FighterStateEnum.CAN_NOT_DAMAGE_BY_ENEMY.state);
    baseState.state = bit.bor(baseState.state, FighterStateEnum.CAN_NOT_DAMAGE_BY_FRIENDLY.state);
    baseState.state = bit.bor(baseState.state, FighterStateEnum.IMMUNITY.state);
    fighter.totalState = baseState;
    -- 英雄配置
    fighter.heroBean = ClientFight.DataManager.t_heroBeanMap[9999];
    -- 体型
    fighter.shape = Rectangle2.New(fighter.heroBean.f_HeroSize);
    -- 警戒范围1
    if (fighter.heroBean.f_WarningRange1 ~= nil) then
        fighter.alertRange1 = Rectangle2.New(fighter.heroBean.f_WarningRange1);
    end
    -- 警戒范围2
    if (fighter.heroBean.f_WarningRange2 ~= nil) then
        fighter.alertRange2 = Rectangle2.New(fighter.heroBean.f_WarningRange2);
    end

    --召唤物相关
    fighter.summons = false;
    -- 初始开始血量
    fighter.hp = 1;
    return fighter;
end
t.New()